﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UpwardsTimeClock.Prototype
{
    public partial class StartSelectForm : Form
    {
        public class ReturnValue
        {
            public TimeSpan StartTime { get; set; }
            public decimal Iteration { get; set; }
        }

        public class StartableNode
        {
            public string Description { get; set; }
            public TimeSpan StartTime { get; set; }
        }

        public static ReturnValue SelectStartTime(decimal iteration)
        {
            StartSelectForm form = new StartSelectForm();
            form.numericIteration.Value = iteration;
            form.ShowDialog();
            ReturnValue returnValue = form._returnValue;
            form.Dispose();
            return returnValue;
        }


        private ReturnValue _returnValue;

        public StartSelectForm()
        {
            InitializeComponent();

            List<StartableNode> startableNodes =
                new List<StartableNode>(Program.Config.Actions.Length);
            foreach (Startable startable in Program.Config.Actions)
            {
                startableNodes.Add(
                    new StartableNode
                    {
                        Description = startable.Description,
                        StartTime = TimeSpan.Parse(startable.StartTime)
                    }
                    );
            }

            startableNodeBindingSource.DataSource = startableNodes;
        }

        private void StartSelectForm_Load(object sender, EventArgs e)
        {

        }

        private void StartTimeGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            StartableNode node = (StartableNode)(StartTimeGrid.Rows[e.RowIndex].DataBoundItem);
            _returnValue = new ReturnValue
                {
                    StartTime = node.StartTime,
                    Iteration = numericIteration.Value
                };
            Close();
        }
    }
}
